#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/4 0004 10:23
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : forredis.py

import redis
import time
import pymysql
import pycurl
import io
import json
from io import StringIO

# import wxpub

# pool = redis.ConnectionPool(host='localhost', password='5tgbnhy67ujm', port=6379)
# r = redis.Redis(connection_pool=pool)
# stu = r.hvals('users')
# con = pymysql.connect(host='localhost', user='root', passwd='root', db='youxian', charset='utf8')
# cursor = con.cursor()
# while True:
#     num=cursor.execute("select user_id from ecs_wxrefun order by id DESC limit 0")
#     if num>0:
#         for row in cursor.fetchall():
#             try:
#                 cursor.execute("update ecs_wxrefun set status=11 where user_id='%d'" % (row))
#                 con.commit()
#             except:
#                 con.rollback()
#     else:
#         time.sleep(2)
#         print("no")
#         pass
# wxxml = ""
res = io.BytesIO()
url = "http://m.kuaidi100.com/query"
str = "type=yunda&postid=3101585865728&id=1&valicode=&temp=0.906445064981233"
# print(str)
# url = "https://api.mch.weixin.qq.com/secapi/pay/refund"
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.HEADER, 0)
# curl.setopt(pycurl.SSLKEYTYPE, "PEM")
# curl.setopt(pycurl.SSLKEY, '/mobile/plugins/payment/cert/apiclient_key.pem')
# curl.setopt(pycurl.SSLCERTTYPE, "PEM")
# curl.setopt(pycurl.SSLCERT, '/mobile/plugins/payment/cert/apiclient_cert.pem')
curl.setopt(pycurl.POST, 1)
curl.setopt(pycurl.POSTFIELDS, str)
curl.setopt(pycurl.WRITEFUNCTION, res.write)
curl.perform()
ce = json.loads(res.getvalue().decode('utf-8'))
print(ce)
test = "<xml><a><![CDATA[a]]></a><b><xml><c><![CDATA[d]]></c><e><![CDATA[f]]></e><g><![CDATA[h]]></g></xml></b></xml>"
# test = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wx2421b1c4370ec43b]]></appid><mch_id><![CDATA[10000100]]></mch_id><nonce_str><![CDATA[NfsMFbUFpdbEhPXP]]></nonce_str><sign><![CDATA[B7274EB9F8925EB93100DD2085FA56C0]]></sign><result_code><![CDATA[SUCCESS]]></result_code><transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id><out_trade_no><![CDATA[1415757673]]></out_trade_no><out_refund_no><![CDATA[1415701182]]></out_refund_no><refund_id><![CDATA[2008450740201411110000174436]]></refund_id><refund_channel><![CDATA[]]></refund_channel><refund_fee>1</refund_fee></xml>"
# import xml.sax
# from mymodule.myfunction import XMLHandler
#
# if (__name__ == "__main__"):
#     xh = XMLHandler()
#     xml.sax.parseString(test, xh)
#     print(xh.getDict())
